package com.gzc.just.play.last.war.sceneserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 玩家成就实体类
 * 
 * 对应数据库表：player_achievements
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Entity
@Table(name = "player_achievements", 
       indexes = {
           @Index(name = "idx_achievement_player_id", columnList = "player_id"),
           @Index(name = "idx_achievement_id", columnList = "achievement_id")
       },
       uniqueConstraints = {
           @UniqueConstraint(name = "uk_player_achievement", columnNames = {"player_id", "achievement_id"})
       })
@EntityListeners(AuditingEntityListener.class)
public class PlayerAchievement {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player_id", nullable = false, foreignKey = @ForeignKey(name = "fk_achievement_player"))
    @JsonIgnore
    private Player player;
    
    @Column(name = "achievement_id", nullable = false)
    private Integer achievementId;
    
    @Column(name = "achievement_name", nullable = false, length = 100)
    private String achievementName;
    
    @Column(name = "progress", nullable = false)
    private Integer progress = 0;
    
    @Column(name = "total_progress", nullable = false)
    private Integer totalProgress = 1;
    
    @Column(name = "is_completed", nullable = false)
    private Boolean isCompleted = false;
    
    @Column(name = "completed_at")
    private LocalDateTime completedAt;
    
    @Column(name = "claimed_rewards", nullable = false)
    private Boolean claimedRewards = false;
    
    @Column(name = "claimed_at")
    private LocalDateTime claimedAt;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // 构造函数
    public PlayerAchievement() {}
    
    public PlayerAchievement(Player player, Integer achievementId, String achievementName, Integer totalProgress) {
        this.player = player;
        this.achievementId = achievementId;
        this.achievementName = achievementName;
        this.totalProgress = totalProgress;
        this.progress = 0;
        this.isCompleted = false;
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Player getPlayer() {
        return player;
    }
    
    public void setPlayer(Player player) {
        this.player = player;
    }
    
    public Integer getAchievementId() {
        return achievementId;
    }
    
    public void setAchievementId(Integer achievementId) {
        this.achievementId = achievementId;
    }
    
    public String getAchievementName() {
        return achievementName;
    }
    
    public void setAchievementName(String achievementName) {
        this.achievementName = achievementName;
    }
    
    public Integer getProgress() {
        return progress;
    }
    
    public void setProgress(Integer progress) {
        this.progress = progress;
    }
    
    public Integer getTotalProgress() {
        return totalProgress;
    }
    
    public void setTotalProgress(Integer totalProgress) {
        this.totalProgress = totalProgress;
    }
    
    public Boolean getIsCompleted() {
        return isCompleted;
    }
    
    public void setIsCompleted(Boolean isCompleted) {
        this.isCompleted = isCompleted;
    }
    
    public LocalDateTime getCompletedAt() {
        return completedAt;
    }
    
    public void setCompletedAt(LocalDateTime completedAt) {
        this.completedAt = completedAt;
    }
    
    public Boolean getClaimedRewards() {
        return claimedRewards;
    }
    
    public void setClaimedRewards(Boolean claimedRewards) {
        this.claimedRewards = claimedRewards;
    }
    
    public LocalDateTime getClaimedAt() {
        return claimedAt;
    }
    
    public void setClaimedAt(LocalDateTime claimedAt) {
        this.claimedAt = claimedAt;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    // 业务方法
    public void updateProgress(int amount) {
        if (!isCompleted) {
            this.progress += amount;
            if (this.progress >= totalProgress) {
                this.progress = totalProgress;
                this.isCompleted = true;
                this.completedAt = LocalDateTime.now();
            }
        }
    }
    
    public void complete() {
        this.isCompleted = true;
        this.progress = totalProgress;
        this.completedAt = LocalDateTime.now();
    }
    
    public void claimRewards() {
        if (isCompleted && !claimedRewards) {
            this.claimedRewards = true;
            this.claimedAt = LocalDateTime.now();
        }
    }
    
    public double getProgressPercentage() {
        if (totalProgress == 0) return 0;
        return (double) progress / totalProgress * 100;
    }
    
    @Override
    public String toString() {
        return "PlayerAchievement{" +
                "id=" + id +
                ", player=" + (player != null ? player.getPlayerId() : null) +
                ", achievementId=" + achievementId +
                ", achievementName='" + achievementName + '\'' +
                ", progress=" + progress +
                ", totalProgress=" + totalProgress +
                ", isCompleted=" + isCompleted +
                ", claimedRewards=" + claimedRewards +
                ", createdAt=" + createdAt +
                '}';
    }
}